package com.hgc.boot01.service.impl;

import com.hgc.boot01.mapper.UserMapper;
import com.hgc.boot01.pojo.User;
import com.hgc.boot01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public int saveUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public User getById(String id) {
        return userMapper.getById(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }


    @Override
    public Map<String,Object> findAllByPage(String val, int currentPage, int pageSize) {
        currentPage = (currentPage - 1) * pageSize;

        Map<String,Object> map = new HashMap<>();

        Long count = userMapper.getCountByUser(val);
        int iCount = count.intValue();
        map.put("count", iCount);

        List<User> userList = userMapper.findAllByPage(val, currentPage, pageSize);
        map.put("userList", userList);

        int pages = (iCount % pageSize) == 0 ? (iCount / pageSize) : ((iCount / pageSize) + 1);
        map.put("pages", pages);

        return map;
    }


}
